package com.cgnpc.scp.common.service;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.client.RestClient;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.vo.CgnRequestHeader;
import cn.com.cgnpc.aep.bizcenter.usercenter.api.entity.StaffTysInfo;
import com.cgnpc.cud.boot.autoconfigure.aep.AepProperties;
import com.cgnpc.framework.utils.CgnHeader;
import com.cgnpc.scp.common.bean.ScpConstants;
import com.cgnpc.scp.common.bean.StaffTysInfoWithQuery;
import com.cgnpc.scp.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据来源<br>
 * 服务名：<br>
 * 中文名称：人员信息微服务<br>
 * 英文名称：aep-hrcenter<br>
 * 详见：相关接口文档<br>
 */
@Service
public class HrCenterService {
    private static final Logger LOGGER = LoggerFactory.getLogger(HrCenterService.class);

    @Autowired
    private AepProperties aepProperties;

    @Autowired
    private RestTemplateService restTemplateService;

    @Autowired
    private RestClient restClient;

    /**
     * post<br>
     * getStaffInfoByKeywordWithPager<br>
     * 根据关键字（姓名、拼音等信息），分页显示获取员工基本信息<br>
     */
    public ApiResult getStaffInfoByKeywordWithPager(StaffTysInfoWithQuery reqEntity) {
        /**
         * getStaffInfoByKeywordWithPager<br>
         * 输入参数 body<br>
         * {
         *     "keyword": "string",
         *     "keywordType": "string",
         *     "pageIndex": 0,
         *     "pageSize": 0
         * }
         * <br>
         * 详细描述<br>
         * 字段中文名称 字段英文名称 参数类型（长度） 说明 是否必填<br>
         * 关键字 可以是姓名、拼音、拼音缩写、员工号、部门、地址、电话、BP keyword string 否<br>
         * 关键字类型 keywordType string 否<br>
         * 当前页 pageIndex integer 默认 1 是<br>
         * 页面大小 pageSize integer 默认 10 是<br>
         *
         */

        List<StaffTysInfo> resultList = null;

        try {
            if (reqEntity == null) {
                return null;
            }

            String reqEntityKeyword = reqEntity.getStaffNo();
            String reqEntityKeywordType = reqEntity.getKeywordType();

            Map<String, Object> reqParamMap = new HashMap<>();
            reqParamMap.put("keyword", StringUtil.formatStringToLen0(reqEntityKeyword).trim());
            reqParamMap.put("keywordType", StringUtil.formatStringToLen0(reqEntityKeywordType).trim());
            reqParamMap.put("pageIndex", formatPageNumWithDefaultValue(reqEntity.getPageNum()));
            reqParamMap.put("pageSize", formatPageSizeWithDefaultValue(reqEntity.getPageSize()));

            // begin 以下调用中台人资服务微服务

            /**
             * 微服务调用地址示例说明<br>
             * 网关地址+服务名+方法名，即如果预生产环境下调用人资中心hrcenter，<br>
             * getStaffTysInfo方法，则调用地址为（其他微服务调用方式一样）：<br>
             * http://aepgw p.gnpjvc.cgnpc.com.cn/hrcenter/getAchievementInfos<br>
             */

            ApiResult apiResult = null;
            // realServiceUrl可能为fileCenter的uploadFiles操作，不要改变该名称
            final String hrCenterServiceMethod = ScpConstants.HRCENTER_URL + "/getStaffInfoByKeywordWithPager";
            String hrCenterUrl = CgnHeader.getUrl(aepProperties.getActive()) + hrCenterServiceMethod;
            LOGGER.info("hrCenterUrl:{};", hrCenterUrl);

            CgnRequestHeader cgnRequestHeader = CgnHeader.getHeader(hrCenterServiceMethod, aepProperties);

            /**
             * 以RequestBody方式请求请使用restClient.postCgnVoForRest方法<br>
             * @see RestClient#postCgnVoForRest(String, CgnRequestHeader, Object)
             */
            apiResult = restClient.postCgnVoForRest(hrCenterUrl, cgnRequestHeader, reqParamMap);
            // end 以下调用中台人资服务微服务

            return apiResult;
        } catch (Exception e) {
            LOGGER.error("error;", e);
            throw new RuntimeException(e);
        }
    }

    private Integer formatPageNumWithDefaultValue(Integer pageNum) {
        return pageNum == null ? Integer.valueOf(10) : pageNum;
    }

    private Integer formatPageSizeWithDefaultValue(Integer pageSize) {
        return pageSize == null ? Integer.valueOf(1) : pageSize;
    }

}
